package com.shop.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shop.Utils.ResponseMessage;
import com.shop.po.Orders;
import com.shop.po.Shipping;
import com.shop.po.User;
import com.shop.service.ShippingService;

@Controller
public class ShippingController {
	
	@Autowired
	private ShippingService shippingService;
	
	@RequestMapping("shipping")
	public String address() {
		return "addresslist";
	}
	
	//显示收货地址列表
	@RequestMapping("/showAddress")
	public String showAddress( Model model, HttpServletRequest request)throws Exception{
		User loginUser = (User) request.getSession().getAttribute("loginUser");
		if (loginUser == null) {
			model.addAttribute("message", "对不起您还没有登录");
			return "msg";
		}
		List<Shipping> shiplist=shippingService.showByUid(loginUser.getUid());
		model.addAttribute("shiplist", shiplist);
		return "addresslist";
	}
	
	//删除收货地址
	@RequestMapping("/delShip")
	public String delShip(Model model, HttpServletRequest request, @RequestParam int id) throws Exception {
		shippingService.deleteByPrimaryKey(id);
		return "redirect:showAddress.action";
	}
	
	//批量删除
	@RequestMapping("/deleteShips")
	@ResponseBody
	public String deleteMessage(String deleteList)throws Exception{
		//因为传递的deleteList为带","的字符串,所以需要消掉分隔符转为数组
		String[] ids = deleteList.split(",");
		for(String id:ids){
			shippingService.deleteByPrimaryKey(Integer.parseInt(id));
		}
		return "1";
	}
	
	//选择收货地址
	@RequestMapping("/chooseShip")
	public String chooseShip(Model model, HttpServletRequest request, @RequestParam int id) throws Exception {
		Shipping ship=shippingService.selectByPrimaryKey(id);
		request.getSession().setAttribute("shipinfo", ship);
		Orders ord=(Orders) request.getSession().getAttribute("orders");
		int oid=ord.getOid();
		return "redirect:payOrderAganin.action?oid="+oid;
	}
	
	//添加收货地址
	@RequestMapping(value = "/addShip", produces = { "application/json;charset=UTF-8" }, method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage addShip(Model model, HttpServletRequest request, Shipping ship) {
		User loginUser = (User) request.getSession().getAttribute("loginUser");
		request.getSession().removeAttribute("shipinfos");
		ship.setUid(loginUser.getUid());
		if(ship.getShipname()==null||ship.getShipname()==""||ship.getShipaddress()==null||ship.getShipaddress()==""
				||ship.getShipphone()==null||ship.getShipphone()=="") {
			request.getSession().setAttribute("shipinfos", ship);
			return ResponseMessage.error().addObject("registMe", "请将信息填写完整!");
		}
		shippingService.addShip(ship);

		return ResponseMessage.success().addObject("registMe", "添加成功!");
	}
	
	//跳转到更新收货地址页面
	@RequestMapping("/updateShip")
	public String updateShip(Model model, HttpServletRequest request, @RequestParam int id) throws Exception {
		Shipping ship=shippingService.selectByPrimaryKey(id);
		model.addAttribute("updShip", ship);
		return "updateShip";
	}
	
	//修改收货地址
	@RequestMapping("/updateAddress")
	public String updateAddress(Model model, HttpServletRequest request, Shipping ship) throws Exception {
		shippingService.updShip(ship);
		return "redirect:showAddress.action";
	}
	
	
}
